<template>
  <div>
    <el-form ref="form" label-width="80px">
      <el-form-item label="SPU名称"> {{ spu.spuName }} </el-form-item>
      <el-form-item label="SKU名称">
        <el-input placeholder="SKU名称" v-model="skuInfo.skuName"></el-input>
      </el-form-item>
      <el-form-item label="价格(元)">
        <el-input
          placeholder="价格(元)"
          v-model.number="skuInfo.price"
          type="number"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量(千克)">
        <el-input placeholder="重量(千克)" v-model="skuInfo.weight"></el-input>
      </el-form-item>
      <el-form-item label="规格描述">
        <el-input
          type="textarea"
          rows="4"
          placeholder="规格描述"
          v-model="skuInfo.skuDesc"
        ></el-input>
      </el-form-item>
      <el-form-item label="平台属性">
        <el-form :inline="true" ref="form" label-width="80px">
          <el-form-item
            :label="attr.attrName"
            v-for="attr in attrInfoList"
            :key="attr.id"
          >
            <el-select placeholder="请选择" v-model="attr.attrIdAndValueId">
              <el-option
                :label="ValueList.valueName"
                :value="`${attr.id}:${ValueList.id}`"
                v-for="ValueList in attr.attrValueList"
                :key="ValueList.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-form :inline="true" label-width="80px">
          <el-form-item
            :label="saleAttr.saleAttrName"
            v-for="saleAttr in spuSaleAttrList"
            :key="saleAttr.id"
          >
            <el-select placeholder="请选择" v-model="saleAttr.attrIdAndValueId">
              <el-option
                :label="attrValue.saleAttrValueName"
                :value="`${saleAttr.id}:${attrValue.id}`"
                v-for="attrValue in saleAttr.spuSaleAttrValueList"
                :key="attrValue.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </el-form-item>
      <el-form-item label="图片列表" border>
        <el-table
          style="width: 100%"
          :data="SupImageList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column prop="prop" width="80" type="selection">
          </el-table-column>
          <el-table-column prop="prop" label="图片" width="width">
            <template slot-scope="{ row }">
              <img :src="row.imgUrl" style="width: 100px; hegiht: 100px" />
            </template>
          </el-table-column>
          <el-table-column prop="imgName" label="名称" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ row }">
              <el-button
                type="primary"
                v-if="row.isDefault == 0"
                @click="changeDefault(row)"
                >设为默认值</el-button
              >
              <el-button type="success" size="mini" v-else>默认</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="save">保存</el-button>
        <el-button @click="changeScene">取消</el-button>
      </el-form-item>
    </el-form>
   
  </div>
</template>

<script>
export default {
  name: "sku",
  data() {
    return {
      SupImageList: [],
      //保存销售属性数据
      spuSaleAttrList: [],
      ////保存平台属性数据
      attrInfoList: [],
      //收集sku字段
      skuInfo: {
        category3Id: 0,
        spuId: 0,
        tmId: 0,

        price: 0,
        weight: "",
        skuAttrValueList: [
          // {
          //   skuId: 0,
          //   valueId: 0,
          // },
        ],
        //默认图片的url地址
        skuDefaultImg: "",
        //商品描述····································
        skuDesc: "",
        skuImageList: [],
        skuName: "",
        skuSaleAttrValueList: [],
      },
      spu: {},
      //收集选中图片的字段
      imageList: [],
     
    };
  },
  methods: {
    //获取数据
    async getDate(row, category1Id, category2Id) {
      this.skuInfo.category3Id = row.category3Id;
      this.skuInfo.spuId = row.id;
      this.skuInfo.tmId = row.tmId;
      this.spu = row;
      let reslut = await this.$API.spu.reqGetImageList(row.id);
      if (reslut.code == 200) {
        let list = reslut.data;
        list.forEach((item) => {
          item.isDefault = 0;
        });
        this.SupImageList = list;
      }
      let reslut1 = await this.$API.spu.reqspuSaleAttrList(row.id);
      if (reslut1.code == 200) {
        this.spuSaleAttrList = reslut1.data;
      }
      let reslut2 = await this.$API.spu.reqAttrInfoList(
        category1Id,
        category2Id,
        row.category3Id
      );
      if (reslut2.code == 200) {
        this.attrInfoList = reslut2.data;
      }
    },
    //取消按钮事件
    changeScene() {
      this.$emit("changescenes", "0");
      //合并对象清除数据
      Object.assign(this._data, this.$options.data());
    },
    //选中图片的回调
    handleSelectionChange(parmas) {
      // 将选择的字段给imageList
      this.imageList = parmas;
    },
    // 设置默认图片的回调
    changeDefault(row) {
      //排他思想
      this.SupImageList.forEach((item) => {
        item.isDefault = 0;
        if (item.id == row.id) {
          item.isDefault = 1;
        }
      });
      //收集默认图片的url地址
      this.skuInfo.skuDefaultImg = row.imgUrl;
    },
    async save() {
      // 将数据结构出来
      const { attrInfoList, skuInfo, spuSaleAttrList, imageList } = this;
      //整理平台属性
      skuInfo.skuAttrValueList = attrInfoList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [attrId, valueId] = item.attrIdAndValueId.split(":");
          prev.push({ attrId, valueId });
        }
        return prev;
      }, []);
      //整理销售属性
      skuInfo.skuSaleAttrValueList = spuSaleAttrList.reduce((prev, item) => {
        if (item.attrIdAndValueId) {
          const [saleAttrId, saleAttrValueId] =
            item.attrIdAndValueId.split(":");
          prev.push({ saleAttrId, saleAttrValueId });
        }
        return prev;
      }, []);
      //整理图片属性
      skuInfo.skuImageList = imageList.map((item) => {
        return {
          imgName: item.imgName,
          imgUrl: item.imgUrl,
          isDefault: item.isDefault,
          spuImgId: item.id,
        };
      });

      let reslut = await this.$API.spu.reqAddskuInfo(skuInfo);
      if (reslut.code == 200) {
        this.$message({
          type: "success",
          message: "添加成功",
        });
        this.$emit("changescenes", 0);
      }
    },
  },
};
</script>

<style></style>
